package com.woniu.inlet.web;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.woniu.command.Command;
import com.woniu.command.equipmentCommand.AddEnergyCommand;
import com.woniu.command.equipmentCommand.DeleteEnergyCommand;
import com.woniu.command.equipmentCommand.UpdateEnergyCommand;
import com.woniu.outlet.po.Energy;
import com.woniu.service.EnergyService;
import com.woniu.util.ResponseResult;
import com.woniu.util.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "能源管理")
@RequestMapping("/equipment/")
public class EnergyController {

    @Autowired
    private EnergyService energyService;

    //查询能源列表
    @GetMapping("findEnergys")
    @ApiOperation("查询全部能源信息")
    public ResponseResult<List<Energy>> findEnergys() throws Exception{
        List<Energy> energys = energyService.findEnergys();
        return new ResponseResult<>(energys, "OK", ResultCode.SUCCESS.getCode());
    }

    //添加能源
    @PostMapping("addEnergy")
    @ApiOperation("添加能源信息")
    @ApiOperationSupport(ignoreParameters = {"id","startDate","endDate","energyNo","spare1","spare2","spare3"})
    public ResponseResult<String> addEnergy(Energy energy){
        Command command = new AddEnergyCommand();
        command.excute(energy);
        return new ResponseResult<>(ResultCode.SUCCESS.getCode(),"添加能源成功");
    }

    //修改能源
    @PostMapping("updateEnergy")
    @ApiOperation("修改能源信息")
    @ApiOperationSupport(ignoreParameters = {"startDate","energyNo","spare1","spare2","spare3"})
    public ResponseResult<String> updateEnergy(Energy energy){
        Command command = new UpdateEnergyCommand();
        boolean flag = (boolean) command.excute(energy);
        if (flag){
            return new ResponseResult<>(ResultCode.SUCCESS.getCode(),"修改能源信息成功");
        }
        return new ResponseResult<>(ResultCode.ID_IS_NOT_EXIST.getCode(),ResultCode.ID_IS_NOT_EXIST.getMessage(),"修改能源信息失败");
    }

    //删除能源
    @DeleteMapping("deleteEnergyById")
    @ApiOperation("删除能源信息(根据能源id)")
    public ResponseResult<String> deleteEnergyById(Long id){
        Command command = new DeleteEnergyCommand();
        boolean flag = (boolean) command.excute(id);
        if (flag){
            return new ResponseResult<>(ResultCode.SUCCESS.getCode(),"删除能源信息成功");
        }
        return new ResponseResult<>(ResultCode.ID_IS_NOT_EXIST.getCode(),ResultCode.ID_IS_NOT_EXIST.getMessage(),"删除能源信息失败");
    }


}
